home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / DIOFNC03.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  4KB  |  102 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   DIOFNC03.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.01.00        February 1991
  6.  *  Language    :   Microsoft C     Version 5.1
  7.  *  Purpose     :   Intel 8255 Compatible Digital IO Functions
  8.  *                  Bit Put Function
  9.  *  ----------------------------------------------------------------------
  10.  *  Revision History:
  11.  *  022891 BVM  :   Change int to short.
  12.  *  070490 BVM  :   Creation
  13.  *  ----------------------------------------------------------------------
  14.  */
  15.  
  16. #define     DIOFNC03_C_DEFINED  1
  17. #include    "DIOLIB.H"
  18. #undef      DIOFNC03_C_DEFINED
  19.  
  20. void dio_bitput (DIODAT *data, short bit, short state);
  21.  
  22. /*- DIO : Bit Put ----------------------------**
  23.  *  Set/Clear one of the bits in the 8255.
  24.  *  A state of !0 sets the bit and a state of 0 clears the bit.
  25.  *  The bit number should be from 0 - 23 as follows:
  26.  *   0 = Port A Bit 0;   7 = Port A Bit 7
  27.  *   8 = Port B Bit 0;  15 = Port B Bit 7
  28.  *  16 = Port C Bit 0;  23 = Port C Bit 7
  29.  *  Passed:
  30.  *      pointer :   DIODAT
  31.  *      short   :   bit number
  32.  *      short   :   state : TRUE (!0) = SET, FALSE (0) = CLEAR
  33.  *  Returns:
  34.  *      nothing
  35.  *      Loads stat with appropriate error code
  36.  */
  37. void dio_bitput (DIODAT *data, short bit, short state)
  38.     {
  39.     short           port;   /* port number  */
  40.     unsigned char   mask;   /* byte mask    */
  41.  
  42.     /*  Make sure the bit requested is valid.
  43.      *  Three ports of 8 bits each = 24 total bits.
  44.      *  This will also validate the port number used later.
  45.      */
  46.     if ( (bit < 0) || (bit > 23) ){
  47.         data->stat = DIO_ST_BB;
  48.         return;
  49.         }
  50.  
  51.     /*  The bit number should have zero offset.
  52.      *  This is useful for lining up the port with integer
  53.      *  divide and it is also used for left shifting to
  54.      *  create a mask.
  55.      *  Get port number by doing integer division.  The port
  56.      *  number should end up as 0, 1, or 2, corresponding to
  57.      *  PORT A, PORT B, and PORT C respectively.
  58.      *  Then do modulo 8 to get the corrsponding bit
  59.      *  number to be used in the particular byte.  This
  60.      *  value will end up being 0 - 7.
  61.      *  Then form the bit mask, starting with 1 and
  62.      *  left shifting to line it up with the proper port bit.
  63.      *  If the bit is to be set, then this is bit ored (|)
  64.      *  with the current port value and then output.  If it is
  65.      *  to be cleared, then the mask bits are inverted and
  66.      *  this vlaue is bit anded (&) with the current port value.
  67.      */
  68.     port = ( bit / 8);      /* port number (0 - 2)          */
  69.     bit = bit % 8;          /* modulo to get the bit number */
  70.     mask = 0x01;            /* assume low bit of byte       */
  71.     mask = mask << bit;     /* shift left to requested bit  */
  72.  
  73.     /*  If bit is to be set:
  74.      *  Switch to appropriate port and set bit.
  75.      */
  76.     if ( state ){
  77.         data->pdat[port] = data->pdat[port] | mask;
  78.         }
  79.  
  80.     /*  Otherwise bit is to be cleared:
  81.      *  Invert bit mask.
  82.      *  Modify appropriate port data by clearing bit.
  83.      */
  84.     else {
  85.         mask = ~mask;
  86.         data->pdat[port] = data->pdat[port] & mask;
  87.         }
  88.  
  89.     /*  Prepare to output data:
  90.      *  Obtain appropriate port data
  91.      *  Obtain port address.
  92.      */
  93.     dio_bput ( data->base + port, data->pdat[port] );
  94.     data->stat = DIO_ST_OK;
  95.     }
  96.  
  97. /*-
  98.  *  ----------------------------------------------------------------------
  99.  *  END DIOFNC03.C Source File
  100.  *  ----------------------------------------------------------------------
  101.  */
  102.